<template>
  <div class="login">
    <el-form class="form" ref="form" :model="form" :rules="rules">
      <div class="image">
        <img src="@/assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="btnClick">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { login } from '@/api/user.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13800000002', //	string	必须		手机号
        password: '123456' //	string	必须		密码
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const req = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              req.test(value)
                ? callback()
                : callback(new Error('请输入正确手机号'))
            }
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '6到12位数字', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    btnClick () {
      this.$refs.form.validate(async result => {
        if (result) {
          await this.$store.dispatch('user/getLogin', this.form)
          this.$message.success('验证成功')
          // 跳转到登录页
          const req = this.$route.query.redirect || '/'
          this.$router.push(req)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url(~@/assets/common/login.jpg);
  display: flex;
  align-items: center;
  justify-content: center;
  .form {
    width: 520px;
    height: 400px;
    .image {
      text-align: center;
      margin-bottom: 10px;
    }
    .btn {
      width: 520px;
      height: 40px;
      background-color: skyblue;
    }
  }
}
</style>
